

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.nn.sparse.sparse &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.nn.sparse.sparse</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.nn.sparse.sparse</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020-2021 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>
<span class="sd">&quot;&quot;&quot;Sparse related tools.&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">operations</span> <span class="k">as</span> <span class="n">P</span>
<span class="kn">from</span> <span class="nn">..cell</span> <span class="kn">import</span> <span class="n">Cell</span>


<div class="viewcode-block" id="SparseToDense"><a class="viewcode-back" href="../../../../api_python/nn/mindspore.nn.SparseToDense.html#mindspore.nn.SparseToDense">[docs]</a><span class="k">class</span> <span class="nc">SparseToDense</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Converts a sparse tensor into dense.</span>

<span class="sd">    In Python, for the ease of use, three tensors are collected into a SparseTensor class.</span>
<span class="sd">    MindSpore uses three independent dense tensors: indices, value and dense shape to represent the sparse tensor.</span>
<span class="sd">    Separate indexes, values and dense shape tensors can be wrapped in a Sparse Tensor object</span>
<span class="sd">    before being passed to the OPS below.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **sparse_tensor** (:class:`mindspore.SparseTensor`): the sparse tensor to convert.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor, converted from sparse tensor.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `sparse_tensor.indices` is not a Tensor.</span>
<span class="sd">        TypeError: If `sparse_tensor.values` is not a Tensor.</span>
<span class="sd">        TypeError: If `sparse_tensor.dense_shape` is not a tuple.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; import mindspore as ms</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor, SparseTensor</span>
<span class="sd">        &gt;&gt;&gt; import mindspore.nn as nn</span>
<span class="sd">        &gt;&gt;&gt; import mindspore.context as context</span>
<span class="sd">        &gt;&gt;&gt; context.set_context(mode=context.PYNATIVE_MODE)</span>
<span class="sd">        &gt;&gt;&gt; indices = Tensor([[0, 1], [1, 2]])</span>
<span class="sd">        &gt;&gt;&gt; values = Tensor([1, 2], dtype=ms.int32)</span>
<span class="sd">        &gt;&gt;&gt; dense_shape = (3, 4)</span>
<span class="sd">        &gt;&gt;&gt; sparse_tensor = SparseTensor(indices, values, dense_shape)</span>
<span class="sd">        &gt;&gt;&gt; sparse_to_dense = nn.SparseToDense()</span>
<span class="sd">        &gt;&gt;&gt; result = sparse_to_dense(sparse_tensor)</span>
<span class="sd">        &gt;&gt;&gt; print(result)</span>
<span class="sd">        [[0 1 0 0]</span>
<span class="sd">         [0 0 2 0]</span>
<span class="sd">         [0 0 0 0]]</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initialize SparseToDense.&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SparseToDense</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sparse_to_dense</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">SparseToDense</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sparse_tensor</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sparse_to_dense</span><span class="p">(</span><span class="n">sparse_tensor</span><span class="o">.</span><span class="n">indices</span><span class="p">,</span>
                                    <span class="n">sparse_tensor</span><span class="o">.</span><span class="n">values</span><span class="p">,</span>
                                    <span class="n">sparse_tensor</span><span class="o">.</span><span class="n">dense_shape</span><span class="p">)</span></div>


<div class="viewcode-block" id="SparseTensorDenseMatmul"><a class="viewcode-back" href="../../../../api_python/nn/mindspore.nn.SparseTensorDenseMatmul.html#mindspore.nn.SparseTensorDenseMatmul">[docs]</a><span class="k">class</span> <span class="nc">SparseTensorDenseMatmul</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Multiplies sparse matrix `a` and dense matrix `b`.</span>
<span class="sd">    The rank of sparse matrix and dense matrix must be equal to `2`.</span>

<span class="sd">    Args:</span>
<span class="sd">        adjoint_st (bool): If true, sparse tensor is transposed before multiplication. Default: False.</span>
<span class="sd">        adjoint_dt (bool): If true, dense tensor is transposed before multiplication. Default: False.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **indices** (Tensor) - A 2-D Tensor, represents the position of the element in the sparse tensor.</span>
<span class="sd">          Support int32, int64, each element value should be non-negative. The shape is :math:`(n, 2)`.</span>
<span class="sd">        - **values** (Tensor) - A 1-D Tensor, represents the value corresponding to the position in the `indices`.</span>
<span class="sd">          Support float16, float32, float64, int32, int64. The shape should be :math:`(n,)`.</span>
<span class="sd">        - **sparse_shape** (tuple) - A positive int tuple which specifies the shape of sparse tensor,</span>
<span class="sd">          should have 2 elements, represent sparse tensor shape is :math:`(N, C)`.</span>
<span class="sd">        - **dense** (Tensor) - A 2-D Tensor, the dtype is same as `values`.</span>
<span class="sd">          If `adjoint_st` is False and `adjoint_dt` is False, the shape must be :math:`(C, M)`.</span>
<span class="sd">          If `adjoint_st` is False and `adjoint_dt` is True, the shape must be :math:`(M, C)`.</span>
<span class="sd">          If `adjoint_st` is True and `adjoint_dt` is False, the shape must be :math:`(N, M)`.</span>
<span class="sd">          If `adjoint_st` is True and `adjoint_dt` is True, the shape must be :math:`(M, N)`.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor, the dtype is the same as `values`.</span>
<span class="sd">        If `adjoint_st` is False, the shape is :math:`(N, M)`.</span>
<span class="sd">        If `adjoint_st` is True, the shape is :math:`(C, M)`.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If the type of `adjoint_st` or `adjoint_dt` is not bool, or the dtype of `indices`,</span>
<span class="sd">            dtype of `values` and dtype of `dense` don&#39;t meet the parameter description.</span>
<span class="sd">        ValueError: If `sparse_shape`, shape of `indices`, shape of `values`,</span>
<span class="sd">            and shape of `dense` don&#39;t meet the parameter description.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; import mindspore as ms</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import nn</span>
<span class="sd">        &gt;&gt;&gt; indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)</span>
<span class="sd">        &gt;&gt;&gt; values = Tensor([1, 2], dtype=ms.float32)</span>
<span class="sd">        &gt;&gt;&gt; sparse_shape = (3, 4)</span>
<span class="sd">        &gt;&gt;&gt; dense = Tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype=ms.float32)</span>
<span class="sd">        &gt;&gt;&gt; sparse_dense_matmul = nn.SparseTensorDenseMatmul()</span>
<span class="sd">        &gt;&gt;&gt; out = sparse_dense_matmul(indices, values, sparse_shape, dense)</span>
<span class="sd">        &gt;&gt;&gt; print(out)</span>
<span class="sd">        [[2. 2.]</span>
<span class="sd">         [6. 6.]</span>
<span class="sd">         [0. 0.]]</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">adjoint_st</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">adjoint_dt</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initialize SparseTensorDenseMatmul&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SparseTensorDenseMatmul</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adj_st</span> <span class="o">=</span> <span class="n">adjoint_st</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adj_dt</span> <span class="o">=</span> <span class="n">adjoint_dt</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sparse_dense_matmul</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">SparseTensorDenseMatmul</span><span class="p">(</span><span class="n">adjoint_st</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">adj_st</span><span class="p">,</span> <span class="n">adjoint_dt</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">adj_dt</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">indices</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">sparse_shape</span><span class="p">,</span> <span class="n">dense</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sparse_dense_matmul</span><span class="p">(</span><span class="n">indices</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">sparse_shape</span><span class="p">,</span> <span class="n">dense</span><span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>